package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.OutRecord;
import com.example.elderlycare.dao.mapper.OutRecordMapper;
import com.example.elderlycare.service.OutRecordService;
import com.example.elderlycare.service.entity.OutRecordItem;
import com.example.elderlycare.service.entity.OutRecordResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OutRecordServiceImpl extends ServiceImpl<OutRecordMapper, OutRecord> implements
        OutRecordService {

    @Resource
    OutRecordMapper outRecordMapper;

    @Override
    public OutRecordResponseBody queryOutRecords(Map<String, Object> params) {
        int total = outRecordMapper.getOutRecordCount(params);
        List<OutRecord> outRecords = outRecordMapper.queryOutRecords(params);
        List<OutRecordItem> outRecordItemList = new ArrayList<>();
        for (OutRecord outRecord : outRecords) {
            OutRecordItem item = OutRecordItem.builder()
                    .id(outRecord.getId())
                    .elderId(outRecord.getElderId())
                    .elderName(outRecord.getElder().getName())
                    .escortName(outRecord.getEscortName())
                    .escortPhone(outRecord.getEscortPhone())
                    .outDate(outRecord.getOutDate())
                    .planReturnDate(outRecord.getPlanReturnDate())
                    .realReturnDate(outRecord.getRealReturnDate())
                    .reason(outRecord.getReason())
                    .isRefund(outRecord.getIsRefund())
                    .isReturn(outRecord.getRealReturnDate() != null ? 1 : 0)
                    .refundDate(outRecord.getRefundDate())
                    .transactor(outRecord.getTransactor())
                    .build();
            outRecordItemList.add(item);
        }
        return OutRecordResponseBody.builder()
                .outRecordItemList(outRecordItemList)
                .total(total)
                .build();
    }
}
